<?php

namespace app\modules\Finance\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\modules\Finance\models\Finance;

/**
 * FinanceSearch represents the model behind the search form about `app\modules\Finance\models\Finance`.
 */
class FinanceSearch extends Finance
{

	public $product_id;   	// 团期ID
	public $product_code;   // 团期编号
	public $order_code;     // 订单编号
	public $order_username; // 订单联系人 
	public $total_price;     	  	// 订单联系人 

	public $equ;     	  	// 订单联系人
    public $min_price;
    public $max_price; 
    public $sort;          //创建时间排序

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'type', 'price_type', 'status', 'user_id', 'total_price'], 'integer'],
            [['company', 'account', 'name', 'desc', 'img', 'created_at', 'updated_at', 'code', 'product_id', 'order_code', 'order_username', 'product_code', 'equ','min_price','max_price','sort'], 'safe'],
        ];
    }

    //  获取关联的订单总金额
    public function getTotalPrice(){
        $price = FinanceOrder::find()->where(['finance_id'=>$this->id])->sum('price');
        return $price;
    }


    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Finance::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' =>  ['defaultOrder'=> ['id'=>SORT_DESC]]
        ]);

        //$query->select("*");
        $query->joinWith(['order']);   // FinanceModel -> getOrder()  FinanceOrder

        $this->load($params);

        if (!$this->validate()) {
            return $dataProvider;
        }

		//  审批项显示范围权限验证
		if(!Yii::$app->user->can('is_finance') && !Yii::$app->user->can('is_admin')){
			$this->user_id = Yii::$app->user->identity->id;
		}


		//if($this->equ == 1){
        //	$query->andFilterWhere([
        //    	'type' => $this->orderPrice,
        //	]);
		//}

		//var_dump($this->order_username);exit;
        $query->andFilterWhere([
            'id' => $this->id,
            'type' => $this->type,
            'price_type' => $this->price_type,
            'status' => $this->status,
            'user_id' => $this->user_id,
            'code' => $this->code,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            'total_price' => $this->total_price,
			// 关联
            //'finance_order.order_code'   => $this->order_code,
        ]);

        $query->andFilterWhere(['like', 'company', $this->company])
              ->andFilterWhere(['like', 'account', $this->account])
              ->andFilterWhere(['like', 'name', $this->name])
              ->andFilterWhere(['like', 'desc', $this->desc])
              ->andFilterWhere(['like', 'finance_order.product_code', $this->product_code])
              ->andFilterWhere(['like', 'finance_order.order_code', $this->order_code])
              ->andFilterWhere(['like', 'img', $this->img]);    

		$query->groupBy("finance_order.finance_id");

        return $dataProvider;
    }

    public function search_all($params)
    {
        $query = Finance::find();

        // add conditions that should always apply here

        // $dataProvider = new ActiveDataProvider([
        //     'query' => $query,
        //     'sort' =>  ['defaultOrder'=> ['id'=>SORT_DESC]]
        // ]);

        //$query->select("*");
        $query->joinWith(['order']);   // FinanceModel -> getOrder()  FinanceOrder

        $this->load($params);

        if (!$this->validate()) {
            return $dataProvider;
        }

        //  审批项显示范围权限验证
        if(!Yii::$app->user->can('is_finance') && !Yii::$app->user->can('is_admin')){
            $this->user_id = Yii::$app->user->identity->id;
        }


        //if($this->equ == 1){
        //  $query->andFilterWhere([
        //      'type' => $this->orderPrice,
        //  ]);
        //}

        //var_dump($this->order_username);exit;
        $query->andFilterWhere([
            'id' => $this->id,
            'type' => $this->type,
            'price_type' => $this->price_type,
            'status' => $this->status,
            'user_id' => $this->user_id,
            'code' => $this->code,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            'total_price' => $this->total_price,
            // 关联
            //'finance_order.order_code'   => $this->order_code,
        ]);

        $query->andFilterWhere(['like', 'company', $this->company])
              ->andFilterWhere(['like', 'account', $this->account])
              ->andFilterWhere(['like', 'name', $this->name])
              ->andFilterWhere(['like', 'desc', $this->desc])
              ->andFilterWhere(['like', 'finance_order.product_code', $this->product_code])
              ->andFilterWhere(['like', 'finance_order.order_code', $this->order_code])
              ->andFilterWhere(['like', 'img', $this->img]);    

        $query->groupBy("finance_order.finance_id");

        return $query->all();
    }

    public function search_excel($params)
    {
        $query = Finance::find();

        // add conditions that should always apply here

        // $dataProvider = new ActiveDataProvider([
        //     'query' => $query,
        // ]);

        if(isset($params['Order'])){
            //$query->join('LEFT JOIN', 'finance_order' , 'finance_order.finance_id=finance.id');
            //$query->orderBy('order.relation, room_id'); //code , 
        }
        //$query->joinWith(['finance_order']);
        $query->joinWith(['order']);   // FinanceModel -> getOrder()

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            //return $dataProvider;
            return false;
        }

        //  审批项显示范围权限验证
        if(!Yii::$app->user->can('is_finance') && !Yii::$app->user->can('is_admin')){
            $this->user_id = Yii::$app->user->identity->id; 
        }
        

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'type' => $this->type,
            'price_type' => $this->price_type,
            'status' => $this->status,
            'user_id' => $this->user_id,
            'code' => $this->code,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,

            // 关联

            //'finance_order.product_id' => $this->product_id,
            //'finance_order.order_code'   => $this->order_code,


        ]);

        if(empty($this->status)){
            $query->andFilterWhere(['not in','status',[4,5]]);
        } 


        $query->andFilterWhere(['like', 'company', $this->company])
            ->andFilterWhere(['like', 'account', $this->account])
            ->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'desc', $this->desc])
			->andFilterWhere(['like', 'finance_order.product_code', $this->product_code])
			->andFilterWhere(['like', 'finance_order.order_code', $this->order_code])
            ->andFilterWhere(['like', 'img', $this->img]);

        return $query->all();
    }
}
